<template>
  <div>
    <el-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :append-to-body="true"
      title="选择类目"
      width="850px"
      @close="close"
    >
      <div class="tree-content">
        <!-- :expand-on-click-node="false"
        :filter-node-method="filterNode"
        :highlight-current="true"-->
        <el-tree
          ref="categoryTree"
          :data="treeData"
          :props="treeProps"
          default-expand-all
          node-key="id"
          show-checkbox
          @node-click="handleTree"
        />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round @click="hide">取 消</el-button>
        <el-button size="small" round type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { categoryTree } from '@/views/wxaMallModule/api/category'

export default {
  data() {
    return {
      // 配置
      treeProps: {
        label: 'title'
      },
      resolve: null,
      reject: null,
      // 状态
      loading: false,
      dialogVisible: null,
      // 数据
      treeData: []
    }
  },
  methods: {
    /**
     * 弹框相关
     */
    open({ categoryIds = [] }) {
      this.getTree(categoryIds)
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
     * 数据组
     */
    // 获取tree列表
    getTree(categoryIds) {
      this.loading = true
      categoryTree()
        .then((res) => {
          this.treeData = res.data
          this.$nextTick(() => {
            console.log(categoryIds)
            this.$refs.categoryTree.setCheckedKeys(categoryIds)
          })
        })
        .catch((err) => {
        })
        .finally(() => {
          this.loading = false
        })
    },

    /**
     * 按钮组
     */
    // 确 定
    submit() {
      const checkIds = this.$refs.categoryTree.getCheckedKeys()
      const categoryList = []
      getCategoryList(this.treeData)
      function getCategoryList(data, titles = []) {
        data.forEach((v) => {
          let names = []
          if (checkIds.includes(v.id)) {
            names = titles.concat([v.title])
            categoryList.push({
              names: names,
              id: v.id,
              title: v.title
            })
          }
          if (v.children && v.children.length > 0) {
            getCategoryList(v.children, names)
          }
        })
      }
      this.resolve && this.resolve(categoryList)
      this.hide()
    },

    // 点击tree
    handleTree(data, node, ref) {
      // this.$refs.categoryTree.setChecked(data, !node.checked);
    },

    /**
     * 状态组
     */

    /**
     * 方法组
     */
    // 筛选tree
    filterNode(value, data) {
      if (!value) return true
      return data.title.indexOf(value) !== -1
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.tree-content {
  overflow: auto;
  max-height: 600px;
  font-size: 14px;
}
</style>
